package com.woniuxy.secondkill.utils;

import com.fasterxml.jackson.databind.ObjectMapper;

import javax.websocket.RemoteEndpoint;
import javax.websocket.Session;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author zhangyong
 * @Date 2021/8/13 0013下午 3:30
 */
public class WebSocketUtil {
    //HashMap：不支持多线程，并发情况下线程不安全
    public static final Map<String, Session> MESSAGEMAP = new ConcurrentHashMap<>();

    //发送消息给客户端
    public static void sendMessage(Session session,String message) {
        if (session!=null) {
            final RemoteEndpoint.Basic basic = session.getBasicRemote();
            if (basic!=null) {
                try {
                    basic.sendText(message);//发送消息回客户端
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //将消息给所有聊天室的人
    //循环发送
    public static void sendMessageToAll(String message) {
        MESSAGEMAP.forEach((sessionId,session)->sendMessage(session, message));
    }

    public static void send(String account,String message){
        try{
            //返回结果：秒杀成功去付钱
            Session session = WebSocketUtil.MESSAGEMAP.get(account);
            //
            ResponseResult responseResult = new ResponseResult();
            responseResult.setMessage(message);
            responseResult.setCode(200);
            //转字符串
            String resultMessage = new ObjectMapper().writeValueAsString(responseResult);
            //发送消息
            WebSocketUtil.sendMessage(session,resultMessage);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
